#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use File::Copy;
use English qw<$PROGRAM_NAME>;


sub ahead($) {
    my($file) = @_;
    print "Heads File: $file\n";
    if (not ($file =~ m/.*(cpp|c)$/)) {
        print "Not c or cpp!\n";
        return;
    }
    my $tar = $file . ".old";
    copy($file, $tar);

    open(INF, "< $tar");
    my @lines = <INF>;
    close(INF);
    open(OUTF, "> $file");

    my $num = @lines - 1;
    print "total: $num\n";
    return if $num <= 0;
    for (;$num>=0; $num--) {
        last if $lines[$num] =~ m/#include/;
    }
    if ($lines[$num+1] =~ m/#endif/) {
        print "Found #endif \n";
        $num ++;
    }

    print "line at $num \n";
    my $set = $num;

    for($num=0; $num<@lines;$num++) {
        print OUTF $lines[$num];
        if ($num == $set) {
            print OUTF '
#define ENTER_FUNC          LOGV(">%s", __func__)
#define LEAVE_FUNC          LOGV("<%s", __func__)
';
        }
    }
    close(OUTF);
}



if ($PROGRAM_NAME eq __FILE__) {
    foreach (@ARGV) {
        ahead("$_");
    }
}
1;
